<template>
    <div class="login-container">
        <div class="login-box">
            <div style="border-radius: 20px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); padding: 40px; background-color: white; margin-left: 100px;">
                <el-form ref="formRef" :rules="rules" :model="form" style="width: 400px;">
                    <div style="text-align: center; margin-bottom: 30px; font-size: 24px;  color: rgb(0, 46, 166); font-weight: bold;">欢 迎 注 册</div>
                    <el-form-item prop="username">
                        <el-input size="large" v-model="form.username" placeholder="请输入账号" prefix-icon="User"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input show-password size="large" v-model="form.password" placeholder="请输入密码" prefix-icon="Lock"></el-input>    
                    </el-form-item>
                    <el-form-item prop="no">
                        <el-input size="large" v-model="form.no" placeholder="请输入工号" prefix-icon="User"></el-input>
                    </el-form-item>
                    <el-form-item prop="confirmPassword">
                        <el-input show-password size="large" v-model="form.confirmPassword" placeholder="请确认密码" prefix-icon="Lock"></el-input>    
                    </el-form-item>
                    <div style="margin-bottom: 20px;">
                        <el-button @click="doRegister" size="large" style="width: 100%;" type="primary">注 册</el-button>
                    </div>
                    <div>已有账号？请<a style="text-decoration: none; color: rgb(0, 46, 166);" href="/register">登录</a></div>
                </el-form>              
            </div>

        </div>
    </div>

</template>
<script setup>
import { useUserStore } from '@/store/userStore';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { registerApi } from '@/api/user';
import { ElMessage } from "element-plus"

const router = useRouter()
const userStore = useUserStore()
const formRef = ref()
const form = ref({
    username: '',
    password: '',
    confirmPassword: ''
})
const validatePass = (rule , value , callback) => {
  if (!value) {
    callback(new Error('请确认密码：'))
  } else if (value !== form.value.password) {
    callback(new Error("两次输入的密码不一致!"))
  } else {
    callback()
  }
}
const rules = {
    username: [
        {
            required: true,
            message: '请输入账号',
            trigger: 'blur'
        }
    ],
    password: [
        {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
        },
        {
            min: 3,
            max: 14,
            message: '密码长度为3-14个字符或数字',
            trigger: 'blur'
        }
    ],
    no: [
        {
            required: true,
            message: '请输入工号',
            trigger: 'blur'
        }
    ],
    confirmPassword:[
        {
            validator: validatePass,
            trigger: 'blur'
        },
        {
            min: 3,
            max: 14,
            message: '密码长度为3-14个字符或数字',
            trigger: 'blur'
        }
    ]

}
const doRegister = () => {
    formRef.value.validate(async (valid) => {
        if(valid) {
            const res = await registerApi(form.value)
            // console.log(userStore.userInfo);
            if(res.code === '200') {
                ElMessage.success("注册成功！")
                router.replace({path: '/login'})                
            } else {
                ElMessage.error(res.msg)
            }

        } 
    })
}
</script>
<style scoped>
.login-container{
    height: 100vh;
    overflow: hidden;
    background-image: url("@/assets/bg.jpg");
    background-size: cover;
}
.login-box{
    width: 50%;
    height: 100%;;
    position: absolute;
    /* top: 50px; */
    display: flex;
    align-items: center;
    right: 0;
}
</style>